2
2
.
.
6
6
.
.
4
4
M
M
o
o
n
n
g
g
o
o
D
D
B
B
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial show how to use Repository to work with MongoDB Database.
No configuration is needed.
By default Hibernate will use anonymous access to connect to localhost at port 27017 and create test database.
If your connection parameters differ you can explicitly set them in application.properties file as shown below.
If you specify database name which doesn't exist, Hibernate will create it for you.
When using MongoDB you have to remove JPA from pom.xml (it only works for SQL DBs like: H2, MySQL, Oracle).
This means that you can't use JPA Annotations like: @Entity, @Table, @Column, @Transient, @Id, @GeneratedValue.
application.properties (choose one if needed)
spring.data.mongodb.url=mongodb://localhost:27017
spring.data.mongodb.uri=mongodb://localhost:27017/mydatabase
spring.data.mongodb.uri=mongodb://myuser:mypassword@localhost:27017/mydatabase
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @RequestMapping (includes Tomcat Server)
NoSQL
Spring Data MongoDB
Enables working with MongoDB
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_db_mongodb (add Spring Boot Starters from the table)
Create Package: entities (inside main package)
– Create Interface: PersonEntity.java (inside package entities)
Create Package: repositories (inside main package)
– Create Class: PersonRepository.java (inside package repositories)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
PersonEntity.java
package com.ivoronline.springboot_db_mongodb.entities;
public class PersonEntity {
public Integer id;
public String name;
public Integer age;
}
MyController
http://localhost:8080/AddPerson
addPerson()
PersonEntity
MongoDB
PersonRepository.java
package com.ivoronline.springboot_db_mongodb.repositories;
import com.ivoronline.springboot_db_mongodb.PersonEntity;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<PersonEntity, Integer> { }
MyController.java
package com.ivoronline.springboot_db_mongodb.controllers;
import com.ivoronline.springboot_db_mongodb.PersonEntity;
import com.ivoronline.springboot_db_mongodb.PersonRepository;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@Autowired
PersonRepository personRepository;
@ResponseBody
@RequestMapping("/AddPerson")
public String addPerson() {
//CREATE PERSON ENTITY
PersonEntity personEntity = new PersonEntity();
personEntity.id = 1;
personEntity.name = "John";
personEntity.age = 20;
//STORE PERSON ENTITY
personRepository.save(personEntity);
//RETURN SOMETHING TO BROWSER
return "Person added to DB";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/AddPerson
Compass Client (MongoDB)
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>